import Vue from 'vue';
import {
  Message
} from 'element-ui';
import {
  MessageBox,
  Notification,
  Loading
} from 'element-ui';
import {
  environment
} from './Environment';

const BASE_URL = environment.BASE_URL;
//未传参
let argumentsLength0 = function (argument) {
  if (argument.length === 0) return;
}
//两个参数
let argumentsLength2 = function (argument) {
  if (argument.length === 2) {
    if (typeof argument[1] === 'function') {
      argument[2] = new Object();
      argument[3] = argument[1];
      return argument;
    }
  }
}
//判断参数是否为空
let paramsIsNull = function (params) {
  if (params === undefined || params === null) {
    return params = new Object();
  }
}

let token = function () {
  Vue.http.interceptors.push((request, next) => {
    if (url !== 'user/login') {
      request.headers.set('token', window.sessionStorage.getItem("token"));
    }
    next((response) => {
      return response
    })
  });
}
//post 请求
let postFun = function (url, params, success, loadingInstance) {
  Vue.http.post(BASE_URL + url, JSON.stringify(params)).then(
    (response) => {
      loadingInstance.close();
      if (response.body.status === '100') {
        success(response.body);
      } else if (response.body.status === '119') {
        Notification.warning("登录过期，请重新登录！");
        setInterval(function () {
          window.location = '/login';
        }, 2000)
      } else {
        if ("string" === (typeof response.body)) {
          let v = JSON.parse(response.body);
          Notification.warning(v.message);
        } else {
          Notification.warning(response.body.message);
        }
      }
    },
    (response) => {
      loadingInstance.close();
      Message.error('系统异常！');
    }
  );
}
// get 
let getFun = function (url, params, success, loadingInstance) {
  Vue.http.get(BASE_URL + url).then(
    (response) => {
      loadingInstance.close();
      if (response.body.status === '100') {
        success(response.body);
      } else if (response.body.status === '119') {
        Notification.warning("登录过期，请重新登录！");
        setInterval(function () {
          window.location = '/login';
        }, 2000)
      } else {
        Notification.warning(response.body.message);
      }
    },
    (response) => {
      alert('fail');
      Message.error('系统异常！');
    }
  );
}
let getJson = function (url, params, success) {
  let result = arguments;
  argumentsLength0(arguments);
  result = argumentsLength2(arguments);
  result[1] = paramsIsNull(params);
  let loadingInstance = Loading.service();
  let mess = getFun(result[0], result[1], result[2], loadingInstance);
  setTimeout(() => {
    loadingInstance.close();
  }, 300);
  return mess;
}
//post 方法
let postJson = function (url, params, success) {
  let result = arguments;
  argumentsLength0(arguments);
  result = argumentsLength2(arguments);
  result[1] = paramsIsNull(params);
  let loadingInstance = Loading.service();
  let mess = postFun(result[0], result[1], result[2], loadingInstance);
  setTimeout(() => {
    loadingInstance.close();
  }, 300);
  return mess;
}
// string
let Trace = function (obj) {
  console.log(JSON.stringify(obj, null, 4));
}
// message show
let Messages = function (message, type) {
  argumentsLength0;
  if (arguments.length === 1) {
    Message({
      showClose: true,
      message: message
    });
    return;
  }
  Message({
    showClose: true,
    message: message,
    type: type
  });
}
//获取权限
let getPermission = function (url) {
  let b = false;
  let GET_PERMISSION_LIST = JSON.parse(window.sessionStorage.getItem("listPermissionFuns"));
  if (GET_PERMISSION_LIST != null) {
    for (let c of GET_PERMISSION_LIST) {
      if (url == c.url) {
        return true;
      } else {
        b = false;
      }

    }
    return b;
  } else {
    return false;
  }
}

module.exports = {
  post: postJson,
  get: getJson,
  trace: Trace,
  message: Messages,
  getPermission: getPermission,
}
